package com.terana.lbs.marketing.web.repository;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.tarena.lbs.pojo.marketing.bo.CouponBO;
import com.tarena.lbs.pojo.marketing.dos.CouponDO;
import com.tarena.lbs.pojo.marketing.po.CouponPO;
import com.tarena.lbs.pojo.marketing.query.CouponQuery;
import com.terana.lbs.marketing.web.mapper.CouponMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class CouponRepository {
    @Autowired
    private CouponMapper couponMapper;

    public PageInfo<CouponPO> getCouponsByPage(CouponQuery query) {
        //只有在 businessId不为空时,SHOP角色商家账号 查询当前商家下的所有优惠券
        QueryWrapper<CouponPO> queryWrapper = new QueryWrapper<>();
        if (query.getBusinessId() != null) {
            queryWrapper.eq("business_id", query.getBusinessId());
        }
        return PageInfo.of(couponMapper.selectList(queryWrapper));
    }

    public void saveCoupon(CouponPO couponPO) {
        couponMapper.insert(couponPO);
    }


    public CouponBO getCouponById(Integer id) {
        CouponDO cDo=couponMapper.selectCouponById(id);
        CouponBO bo=null;
        if (cDo!=null){
            bo=new CouponBO();
            BeanUtils.copyProperties(cDo,bo);
        }
        return bo;
    }

    public List<Integer> getAvailableCouponIds() {
        return couponMapper.getCouponIdsByTime();
    }
}

